package cn.com.gridinfo.sooc.core.expand.score.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.jeremy.arad.Arad;
import com.jeremy.arad.utils.AnimUtil;
import com.jeremy.arad.utils.Log;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import butterknife.OnItemClick;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.base.BaseSoocActivity;
import cn.com.gridinfo.sooc.common.UrlConst;
import cn.com.gridinfo.sooc.core.expand.score.adapter.GridTxAdapter;
import cn.com.gridinfo.sooc.core.expand.score.bean.Rate;
import cn.com.gridinfo.sooc.core.expand.score.common.CodeConst;
import cn.com.gridinfo.sooc.core.expand.score.dao.ScoreDao;
import cn.com.gridinfo.sooc.utils.HttpUrl;
import cn.com.gridinfo.sooc.utils.IntentUtil;
import cn.com.gridinfo.sooc.utils.MessageUtils;

/**
 * 提现详情界面
 *
 * @author yingchunli
 */
public class TxInfoActivity extends BaseSoocActivity {

    @InjectView(R.id.toolbar_righttv)
    TextView toolbarRightText;//tooltbar右侧文字
    @InjectView(R.id.grid_tx)
    GridView gridTx;//提现方式的容器
    @InjectView(R.id.jine)
    TextView jkCounts;//可提现金额
    @InjectView(R.id.zhifubao_zhanghao_text)
    TextView zfb;//支付宝账号
    @InjectView(R.id.qubangding)
    TextView btnBind;//去绑定按钮
    private String txRules;//提现说明字符串
    private GridTxAdapter adapter;//适配器
    private List<Rate> list;//提现方式的集合
    private String txje;//要提现金额
    ScoreDao dao;
    @InjectView(R.id.tx_btn)
    Button txBtn;//提现按钮
    String jifen;//积分细节
    int ktje;//可提现金额
    private String zhmc;//支付宝账户名称
    private static final int INTENT_UPDATE_ZFB = 1;//跳转到修改支付宝页面的常量
    int isSuccess = 0;//提现规则是否加载完成，默认未0，表示没有加载完成

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.expand_score_txinfo);
        ButterKnife.inject(this);
        dao = new ScoreDao(this);
        toolbarRightText.setVisibility(View.VISIBLE);//显示tooolbar右侧文字
        toolbarRightText.setText("提现说明");//设置文字
        //获取兑换账户
        dao.getMyAccount(CodeConst.EXPAND_SCORE_GET_MYACCOUNT);
        //获取提现规则
        dao.getTx(CodeConst.EXPAND_SCORE_RULE);
    }

    /**
     * 初始化界面数据
     */
    private void initview() {
        list = new ArrayList<>();
        load();//添加数据
        adapter = new GridTxAdapter(this, list);
        gridTx.setAdapter(adapter);
        //获取可提现金额
        ktje = Integer.parseInt(Arad.preferences.getString("cash"));
        jkCounts.setText(ktje + "元");
        int jft = Integer.parseInt(Arad.preferences.getString("score")) - Integer.parseInt(Arad.preferences.getString("appScore"));

        //获取提现规则
        dao.getTxRules(CodeConst.GET_RULES);

        jifen = "当前积分总数: " + Arad.preferences.getString("score") + "\n" + "其中应用体验积分: " + Arad.preferences.getString("appScore") + ",  "
                + "其他积分: " + jft;

        txBtn.setEnabled(false);
    }

    //添加数据
    private void load() {
        list.addAll(dao.getRateList());
    }

    @Override
    public void onRequestSuccess(int requestCode) {
        //关闭进度条
        showProgress(false, true);
        try {
            if (dao.getStatus().equals("0")) {
                switch (requestCode) {
                    case CodeConst.GET_RULES://获取提现规则
                        txRules = dao.getData();
                        txRules = txRules.replace("<p>", "");
                        txRules = txRules.replace("</p>", "\n");
                        txRules = txRules.replace("\"", "");
                        Log.d("提现规则-》" + txRules);
                        isSuccess = 1;
                        break;
                    case CodeConst.EXPAND_SCORE_GET_MYACCOUNT:
                        //获取兑换账户
                        if (dao.getTxzhList() != null && dao.getTxzhList().size() > 0) {
                            zfb.setText(dao.getTxzhList().get(0).getZh());//设置支付宝账户
                            zhmc = dao.getTxzhList().get(0).getZhmc();
                            btnBind.setVisibility(View.GONE);//不显示去绑定按钮
                        } else {
                            //为绑定支付宝账号，提现按钮不可点击
                            txBtn.setEnabled(false);
                        }
                        break;
                    case CodeConst.EXPAND_SCORE_GOTX://提现成功
                        MessageUtils.showRightImageShortToast(this, "提现成功");
                        //提现成功需要更新我的积分等信息     获取我的积分
                        dao.getMyCounts(CodeConst.GET_MYCOUNTS);
                        break;

                    case CodeConst.EXPAND_SCORE_RULE:
                        //获取兑换规则
                        initview();
                        break;
                    case CodeConst.GET_MYCOUNTS:
                        //成功获取我的积分
                        //提现说明中在提现成功之后，积分需要修改
                        int jft = Integer.parseInt(Arad.preferences.getString("score")) - Integer.parseInt(Arad.preferences.getString("appScore"));
                        jifen = "当前积分总数: " + Arad.preferences.getString("score") + "\n" + "其中应用体验积分: " + Arad.preferences.getString("appScore") + ", "
                                + "其他积分: " + jft;
                        break;
                }
            } else {
                MessageUtils.showImageShortToast(this, dao.getStatusCode());
            }
        } catch (Exception e) {
            MessageUtils.showImageShortToast(this, "请求数据异常");
        }
    }

    /**
     * 支付宝区域点击
     */
    @OnClick(R.id.zfb_layout)
    void zfbClick() {
        if (btnBind.getVisibility() == View.VISIBLE) {
            //未绑定支付宝账号
            //跳转到去绑定界面
            IntentUtil.intentSlidIn(this, BindZFBActivity.class, CodeConst.INTENT_BIND);
        } else {
            //绑定支付宝账号，之后，   点击跳转到修改支付宝账号页面
            Intent intent = new Intent(this, BindZFBActivity.class);
            intent.putExtra("ZFB_ZH", zfb.getText().toString().trim());//将支付宝的账户传出去
            intent.putExtra("ZFB_XM", zhmc);//将支付宝的账户名称传出
            startActivityForResult(intent, INTENT_UPDATE_ZFB);//绑定成功后刷新一下
            AnimUtil.intentSlidIn(this);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case CodeConst.INTENT_BIND:
                //从绑定支付宝界面返回
                zfb.setText(data.getStringExtra("zfb"));
                txBtn.setEnabled(true);//提现按钮可点击，有支付宝账号
                break;
            case INTENT_UPDATE_ZFB:
                //从修改支付宝账号回来
                //获取提现账号
                dao.getMyAccount(CodeConst.EXPAND_SCORE_GET_MYACCOUNT);
                break;
        }
    }

    /**
     * GridView 的单项点击
     */
    @OnItemClick(R.id.grid_tx)
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //确定提现金额，跳转
        txje = list.get(position).getJe() + "";
        Log.d("要提现的金额：" + txje);
        adapter.selectRefresh(position);
        if (btnBind.getVisibility() == View.GONE) {
            txBtn.setEnabled(true);
        } else {
            txBtn.setEnabled(false);
        }
    }

    /**
     * 提现的点击
     */
    @OnClick(R.id.tx_btn)
    void onTxClick() {
        //去提现
        if (ktje >= Integer.parseInt(txje.toString().trim())) {
            dao.sendTxIfo(Integer.parseInt(txje), zfb.getText().toString().trim(), CodeConst.EXPAND_SCORE_GOTX);
        } else {
            MessageUtils.showImageShortToast(this, "积分不足,不能提现!");
        }
    }

    @Override
    public String setupToolBarTitle() {
        return "提现详情";
    }

    @Override
    public boolean setupToolBarLeftButton(ImageView leftButton) {
        return true;
    }

    @OnClick(R.id.toolbar_leftbtn)
    void onToolbarLeftClick() {
        finish();
    }

    @OnClick(R.id.toolbar_righttv)
    void onToolbarRightClick() {
        //显示提现说明
        new MaterialDialog.Builder(this)
                .title("提现说明")
                .content(jifen + "\n\n" + "提现规则:\n" + txRules)
                .titleColor(Color.BLACK)
                .contentColor(Color.BLACK)
                .backgroundColor(Color.WHITE)
                .positiveText(R.string.agree)
                .positiveColor(getResources().getColor(R.color.colorAccent))
                .cancelable(true)
                .show();
        //判断提现规则是否加载成功
        if (isSuccess == 0) {
            //显示加载进度条
            showProgress(true, true);
        }
    }

    @Override
    public boolean setToolbarRighttv(TextView toolbarRighttv) {
        return true;
    }
}
